﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROS.Persistence;

namespace ROS.Application.DomainObjects
{
    internal class Order : DomainObject
    {
        #region Fields

        private long id;
        private int tableNumber;
        private int guestCount;
        private String notes;
        private List<OrderItem> orderItems;
        private OrderEvent creationEvent;
        private OrderEvent completionEvent;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the ID of this Order.
        /// </summary>       
        public long ID
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// Gets or sets the table number this order originated from.
        /// </summary>
        public int TableNumber
        {
            get { return tableNumber; }
            set { tableNumber = value; }
        }

        /// <summary>
        /// Gets or sets the number of guests on this order.
        /// </summary>
        public int GuestCount
        {
            get { return guestCount; }
            set { guestCount = value; }
        }

        /// <summary>
        /// Get or set the notes on this order.
        /// </summary>
        public String Notes
        {
            get { return notes; }
            set { notes = value; }
        }

        /// <summary>
        /// Gets a list of items for this order.
        /// </summary>
        /// <remarks>
        /// I am breaking encapulation of Persistence to implement lazy loading here. Later
        /// this should be refactored to be gathered via a virtual proxy.
        /// </remarks>
        public IList<OrderItem> OrderItems
        {
            get 
            {
                return orderItems;
            }
        }

        /// <summary>
        /// Get the user who entered this order.
        /// </summary>
        public User EnteredBy
        {
            get { return creationEvent.PerformedBy; }
            set {
                if (creationEvent == null)
                    creationEvent = new OrderEvent(id, value, OrderEventType.ADDED, DateTime.Now);
                else
                    creationEvent.PerformedBy = value;
            }
        }

        /// <summary>
        /// Gets the date and time this order was entered.
        /// </summary>
        public DateTime DateTimeEntered
        {
            get { return creationEvent.TimeOccurred; }
        }

        /// <summary>
        /// Get the user who completed this order.
        /// </summary>
        public User CompletedBy
        {
            get { return completionEvent.PerformedBy; }
        }

        /// <summary>
        /// Gets the date and time this order was completed.
        /// </summary>
        public DateTime DateTimeCompleted
        {
            get { return completionEvent.TimeOccurred; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new Order object.
        /// </summary>
        /// <param name="tableNumber"></param>
        /// <param name="guestCount"></param>
        /// <param name="notes"></param>
        public Order(int tableNumber, int guestCount, String notes)
        {
            this.tableNumber = tableNumber;
            this.guestCount = guestCount;
            this.notes = notes;
            this.orderItems = null;
        }

        /// <summary>
        /// Construct a new Order object.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tableNumber"></param>
        /// <param name="guestCount"></param>
        /// <param name="notes"></param>
        public Order(long id, int tableNumber, int guestCount, String notes)
        {
            this.id = id;
            this.tableNumber = tableNumber;
            this.guestCount = guestCount;
            this.notes = notes;
            this.orderItems = null;
            this.creationEvent = null;
            this.completionEvent = null;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Calculate the total price for this order.
        /// </summary>
        /// <returns></returns>
        public Money CalculateOrderPrice(Currency currency)
        {
            Money orderPrice = new Money(0, currency);
            //IList<OrderItem> orderItems = OrderItems;
            //foreach (OrderItem orderItem in orderItems)
            //{
            //    orderPrice += orderItem.Price;
            //}
            return orderPrice;
        }

        #endregion

        internal void AddOrderItem(OrderItem orderItem)
        {
            throw new NotImplementedException();
        }
    }
}
